import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
import { immer } from 'zustand/middleware/immer'
interface MainStore {
    token: string
    setToken: (token: string) => void
    theme: 'light' | 'dark'
    setTheme: (theme: 'light' | 'dark') => void
    permissions: string[]
    setPermissions: (permissions: string[]) => void
}
const useMainStore = create<MainStore>()(
    immer(
        persist((set) => ({
            token: '',
            setToken: (token: string) => set({ token }),
            theme: 'light',
            setTheme: (theme: 'light' | 'dark') => set({ theme }),
            permissions: [],
            setPermissions: (permissions: string[]) => set({ permissions }),
        }),
            {
                name: 'main-store',
                storage: createJSONStorage(() => localStorage),
                partialize: (state) => ({
                    token: state.token,
                    theme: state.theme,
                    permissions: state.permissions,
                }) // 部分状态持久化 默认全部持久化
            }
        )
    )
)

export default useMainStore;
